<?php

function weightMap() {
    return array(
            "A" => 71.0788,
            "R" => 156.1875,
            "N" => 114.1038,
            "D" => 115.0886,
            "C" => 103.1388,
            "E" => 129.1155,
            "Q" => 128.1307,
            "G" => 57.0519,
            "H" => 137.1411,
            "I" => 113.1594,
            "L" => 113.1594,
            "K" => 128.1741,
            "M" => 131.1926,
            "F" => 147.1766,
            "P" => 97.1167,
            "S" => 87.0782,
            "T" => 101.1051,
            "W" => 186.2132,
            "Y" => 163.1760,
            "V" => 99.1326
            );
}

function theoSpec($pepid) {
    //query peptide
    $query = "SELECT sequence FROM peptide WHERE id=$pepid";
    $result = mysql_query($query) or die('Error, query failed'.$query);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    if (!$row) return array();
    $seq = $row['sequence'];
    
    // query site
    $query = "SELECT locationInPeptide,mass FROM peptideSiteRelation,site,ptm WHERE peptide=$pepid AND peptideSiteRelation.site=site.id AND site.ptm=ptm.label ORDER BY locationInPeptide";
    $result = mysql_query($query) or die('Error, query failed'.$query);
    $sites = array();
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $sites[$row['locationInPeptide']] = $row['mass'];
    }

    $map = weightMap();

    $len = strlen($seq);
    //b ions
    $mass = 1.0;
    for ($i=0; $i<$len; $i++) {
        $w = $map[$seq[$i]];
        $mass += $w;
        if ($sites[$i]) {
            $mass += $sites[$i];
        }
        $bions[] = $mass;
    }

    // yions
    $mass = 19.0;
    for ($i=$len-1; $i>=0; $i--) {
        $w = $map[$seq[$i]];
        $mass += $w;
        if ($sites[$i]) {
            $mass += $sites[$i];
        }
        $yions[] = $mass;
    }

    return array($bions,$yions);
}

?>
